//
//  LLTabBar.m
//  LLRiseTabBarDemo
//
//  Created by Meilbn on 10/18/15.
//  Copyright © 2015 meilbn. All rights reserved.
//

#import "LLTabBar.h"

#define SCREEN_WIDTH    [UIScreen mainScreen].bounds.size.width

@interface LLTabBar ()
@end

@implementation LLTabBar

#pragma mark - Lifecycle
- (instancetype)initWithFrame:(CGRect)frame {
	self = [super initWithFrame:frame];
	
	if (self) {
		[self config];
	}
	
	return self;
}

#pragma mark - Private Method
- (void)config {
	self.backgroundColor = [UIColor whiteColor];
    UIImageView *backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -17, SCREEN_WIDTH, self.bounds.size.height + 17)];
    backImageView.image = [UIImage imageNamed:@"tabbarBackImage"];
    [self addSubview:backImageView];
    
    if (([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)) {
        UIImageView *addBackImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - 17, SCREEN_WIDTH, self.bounds.size.height + 17)];
        addBackImageView.image = [UIImage imageNamed:@"tabbarBackImage"];
        [self addSubview:addBackImageView];
    }
}

//设置选中的控制器
- (void)setSelectedIndex:(NSInteger)index {
    
    UITabBarController *tabBarController = (UITabBarController *)_delegate;
	for (LLTabBarItem *item in self.tabBarItems) {
		if (item.tag == index) {
			item.selected = YES;
		} else {
			item.selected = NO;
		}
	}
	
	if (tabBarController) {
		tabBarController.selectedIndex = index;
	}
}

#pragma mark - Touch Event
- (void)itemSelected:(LLTabBarItem *)sender {
    sender.highlighted = NO;
    [self setSelectedIndex:sender.tag];
    if (self.delegate) {
        if ([self.delegate respondsToSelector:@selector(tabBarDidSelectedRiseButton)]) {
            [self.delegate tabBarDidSelectedRiseButton];
        }
    }
}

#pragma mark - Setter
- (void)setTabBarItemAttributes:(NSArray<NSDictionary *> *)tabBarItemAttributes {
    
	_tabBarItemAttributes = tabBarItemAttributes.copy;
    NSAssert(_tabBarItemAttributes.count > 2, @"TabBar item count must greet than two.");
    CGFloat tabBarHeight = CGRectGetHeight(self.frame);
	NSInteger itemTag = 0;
    _tabBarItems = [NSMutableArray arrayWithCapacity:_tabBarItemAttributes.count];
    
	for (id item in _tabBarItemAttributes) {
		if ([item isKindOfClass:[NSDictionary class]]) {
            NSDictionary *itemDict = (NSDictionary *)item;
            LLTabBarItemType type = [itemDict[kLLTabBarItemAttributeType] integerValue];
            CGRect frame = CGRectMake(itemTag * (SCREEN_WIDTH/5.0),
                                      0,
                                      SCREEN_WIDTH/5.0,
                                      tabBarHeight);
            
            LLTabBarItem *tabBarItem = [self tabBarItemWithFrame:frame
                                                           title:itemDict[kLLTabBarItemAttributeTitle]
                                                 normalImageName:itemDict[kLLTabBarItemAttributeNormalImageName]
                                               selectedImageName:itemDict[kLLTabBarItemAttributeSelectedImageName]
                                                  tabBarItemType:type];
			if (itemTag == 2) {
				tabBarItem.selected = YES;
			}
            
			[tabBarItem addTarget:self action:@selector(itemSelected:) forControlEvents:UIControlEventTouchUpInside];
            [tabBarItem addTarget:self action:@selector(hightLighted:) forControlEvents:UIControlEventAllEvents];
            
            tabBarItem.tag = itemTag;
            itemTag++;
        
            [_tabBarItems addObject:tabBarItem];
			[self addSubview:tabBarItem];
		}
	}
}

- (void)hightLighted:(UIButton *)sender{
    sender.highlighted = NO;
}

- (LLTabBarItem *)tabBarItemWithFrame:(CGRect)frame
                                title:(NSString *)title
                      normalImageName:(NSString *)normalImageName
                    selectedImageName:(NSString *)selectedImageName
                       tabBarItemType:(LLTabBarItemType)tabBarItemType {
    
    LLTabBarItem *item = [[LLTabBarItem alloc] initWithFrame:frame];
    [item setTitle:title forState:UIControlStateNormal];
    [item setTitle:title forState:UIControlStateSelected];
    item.titleLabel.font = [UIFont systemFontOfSize:12];
    UIImage *normalImage = [UIImage imageNamed:normalImageName];
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    [item setImage:normalImage forState:UIControlStateNormal];
    [item setImage:selectedImage forState:UIControlStateSelected];
    [item setTitleColor:[UIColor colorWithWhite:255 / 255.0 alpha:1] forState:UIControlStateNormal];
    [item setTitleColor:[UIColor yellowColor] forState:UIControlStateSelected];
    item.tabBarItemType = tabBarItemType;
    
    return item;
}

@end
